#include "pch.h"
#include "GRTXBVH.h"

HitInfo GRTXBVH::Intersect(const GRTXRay& ray)const
{
	if (box.Intersect(ray.Orgin,ray.Dir))
	{
		// 抵达叶子节点
		if (IsLeaf())
		{
			return IntersectSet(ray, primitives);
		}
		HitInfo l = left->Intersect(ray);
		HitInfo r = right->Intersect(ray);
		
		auto t =  (l.t < r.t) ? l : r;
		if (t.t<=0)
		{
			l.t = 0;
		}
		return t;
	}
	else
		return HitInfo();
}

